package com.yc.cloud.openai.portal.controller;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.openai.entity.GptDatasetCollection;
import com.yc.cloud.openai.portal.dto.gpt.request.GptDatasetCollectionListParam;
import com.yc.cloud.openai.portal.dto.request.GptDatasetCollectionCreateRequest;
import com.yc.cloud.openai.portal.dto.request.GptDatasetCollectionDeleteRequest;
import com.yc.cloud.openai.portal.enums.GPTChatProvider;
import com.yc.cloud.openai.portal.service.GptDatasetCollectionService;
import com.yc.cloud.openai.portal.vo.gpt.GptDatasetCollectionVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.val;

/**
 * 知识库内容集合
 * 
 * @author Yancy
 * @date 2025-01-08
 */
@RestController
@RequestMapping("chat/dataset/collection")
@Api(value = "知识库内容集合", tags = "知识库内容集合")
public class GptDatasetCollectionController {

        @Resource
        private GptDatasetCollectionService gptDatasetCollectionService;

        @GetMapping
        @ApiOperation(value = "知识库内容集合列表", notes = "支持分页查询,字段模糊匹配,排序等")
        public ResponseResult<CommonPage<GptDatasetCollectionVo>> list(@Validated BaseQueryParam queryParam,
                        GptDatasetCollectionListParam param) {
                val result = gptDatasetCollectionService.list(queryParam, param);
                return ResponseResult.success(CommonPage.restPage(result));
        }

        @GetMapping("{id}")
        @ApiOperation(value = "知识库内容集合详情", notes = "根据ID查询")
        public ResponseResult<GptDatasetCollection> get(
                        @PathVariable @ApiParam(value = "ID", required = true) Long id) {
                val result = gptDatasetCollectionService.get(id);
                return ResponseResult.success(result);
        }

        @PostMapping
        @ApiOperation(value = "添加知识库内容集合", notes = "添加")
        public ResponseResult<GptDatasetCollectionVo> create(
                        @Validated GptDatasetCollectionCreateRequest param,
                        @RequestPart("file") MultipartFile file) {
                val result = gptDatasetCollectionService.create(param, file);
                return result != null ? ResponseResult.success(result) : ResponseResult.failed();
        }

        // @PostMapping
        // @ApiOperation(value = "添加知识库内容集合", notes = "添加")
        // public ResponseResult<GptDatasetCollection> create(
        // @Validated @RequestBody GptDatasetCollectionCreateRequest param) {
        // val result = gptDatasetCollectionService.create(param);
        // return result != null ? ResponseResult.success(result) :
        // ResponseResult.failed();
        // }

        @ApiOperation(value = "删除知识库内容集合", notes = "通过ID删除")
        @DeleteMapping(value = "{id}")
        public ResponseResult<Boolean> delete(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
                val param = new GptDatasetCollectionDeleteRequest();
                param.setProvider(GPTChatProvider.FASTGPT.getKey());
                val result = gptDatasetCollectionService.delete(id, param);
                return result ? ResponseResult.success(true) : ResponseResult.failed();
        }

        @ApiOperation(value = "修改知识库内容集合", notes = "通过ID修改")
        @PutMapping(value = "{id}")
        public ResponseResult<GptDatasetCollection> update(
                        @PathVariable @ApiParam(value = "ID", required = true) Long id,
                        @Validated @RequestBody GptDatasetCollection param) {
                val result = gptDatasetCollectionService.update(id, param);
                return result != null ? ResponseResult.success(result) : ResponseResult.failed();
        }
}
